﻿using System;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        msg.ForeColor = System.Drawing.Color.Red;
        //添加新闻事件
        newList.ItemCommand +=
             new DataGridCommandEventHandler(this.showNewInfo);
        getNewList();
    }

    protected void getNewList()
    {
        String coursesql = "SELECT TOP (5) ID,title, date FROM (SELECT ID,title, Replace(CONVERT(varchar, date, 111),'/','-') AS date FROM new) AS NEWLIST ORDER BY ID DESC";  
        DataSet newDataSet = DataBase.Query(coursesql);
        newList.DataSource = newDataSet.Tables[0].DefaultView;
        newList.DataBind();
    }

    protected void showNewInfo(Object sender, DataGridCommandEventArgs e)
    {
        switch (((LinkButton)e.CommandSource).CommandName)
        {
            case "showNew":
                newInfo(sender, e);
                break;
            default:
                break;

        }

    }
    protected void newInfo(object sender, DataGridCommandEventArgs e)
    {
        LinkButton selectButton = (LinkButton)e.CommandSource;
        String newTitle = selectButton.Text;
        Session.Add("newTitle", newTitle);
        Server.Transfer("ShowNew.aspx");
    }
    
    //学生登陆
    protected void studentLogin_Click()
    {
        Cstudent stu = Cstudent.selectStudent(name.Text);
        if (stu == null)
        {
            msg.Text = "登录错误！此学号不存在";
        }
        else if (stu.studentPassword.Equals(password.Text.PadRight(50)))
        {
            Session.Add("studentData", stu);
            //判断能否选课
            if (Ccontrol.getEnrollinState() == 1)
            {
                Response.Redirect("student.aspx");
            }
            else if (Ccontrol.getEnrollinState() == 0)
            {
                Response.Redirect("student/student.aspx");
            }
            else if (Ccontrol.getEnrollinState() == 3)
            {
                Response.Redirect("error.aspx");
            }
        }
        else
        {
            msg.Text = "登录错误！密码不正确";
            return;
        }
        
    }
    //教师登陆
    protected void teacherLogin_Click()
    {
        if (name.Text == "" || password.Text == "")
        {
            msg.Text = "登录错误！信息不完整";
            return;
        }
        SqlConnection Con = new SqlConnection("Data Source=172.16.36.221;Initial Catalog=在线选课系统;user id=sa;pwd=Call3ter;");
        string sel = "select * from Teacher where teacherName='" + name.Text + "'";
        SqlCommand addCommand = new SqlCommand(sel, Con);
        addCommand.Connection.Open();
        SqlDataReader selreader;
        selreader = addCommand.ExecuteReader();
        if (selreader.Read() == false)
        {
            //msg.Text = "该教师不存在";
            msg.Text = "登录错误！用户名不存在";
            return;
        }
        else
        {
            string getpas = selreader.GetString(2);
            string getname = selreader.GetString(1);
            int getid = selreader.GetInt32(0);
            addCommand.Connection.Close();
            if (getpas.CompareTo(password.Text.PadRight(50)) == 0)
            {
                Session["teacher"] = getname;
                Session["teacherID"] = getid;
                Response.Redirect("teacher/teacher.aspx");
                //Server.Transfer("teacher/teacher.aspx");
            }
            else
                msg.Text = "登录错误！密码不正确";
        }
    }

    //根据教师或者学生判断登陆页面
    protected void Login_Click(object sender, ImageClickEventArgs e)
    {
        if (userGroup.Text.Equals("学生"))
        {
            studentLogin_Click();
        }
        else if (userGroup.Text.Equals("教师"))
        {
            teacherLogin_Click();
        }
    }
}
